package net.appportal.ant.tasks;

import java.util.Vector;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.TaskContainer;

/**
 * 
 * <property name="backup.selected" append=", appendtest"/>
 * 
 * <stringbuilder name="backup.selected" append=", appendtest"/>
 * 
 * <stringbuilder value="${deploy.selectedsystem}" contain="${subsystem.main}," property="deploy.includescript"/>
 * <stringbuilder name="deploy.includes" string="," replace="/**,"/>
 * 
 * @author gdragon
 */
public class StringBuilderTask extends Task implements TaskContainer
{
	private Vector tasks = new Vector();
	private String append = null;
	private String name = null;
	private String value = null;
	private String delimeter = "";
	private String indexof = null;
	private String replace = null;
	private String verbose = "false";
	private String trim = "false";
	private String string = null;
	private String contain = null;
	private String property = null;
	
	/**
	 * 
	 *
	 */
	public StringBuilderTask()
	{
		
	}
	
	/**
	 * 
	 * @param name
	 */
	public void setTrim( String trim )
	{
		this.trim = trim;
	}
	
	/**
	 * 
	 * @param name
	 */
	public void setName( String name )
	{
		this.name = name;
	}
	
	/**
	 * 
	 * @param value
	 */
	public void setValue( String value )
	{
		this.value = value;
	}
	
	/**
	 * 
	 * @param property
	 */
	public void setProperty( String property ) 
	{
		this.property = property;
	}	

	/**
	 * 
	 * @param append
	 */
	public void setAppend( String append )
	{
		this.append = append;
	}
	
	/**
	 * 
	 * @param replace
	 */
	public void setReplace( String replace )
	{
		this.replace = replace;
	}

	/**
	 * 
	 * @param string
	 */
	public void setString( String string )
	{
		this.string = string;
	}

	/**
	 * 
	 * @param include
	 */
	public void setContain( String contain )
	{
		this.contain = contain;
	}	
	
	/**
	 * 
	 * @param replace
	 */
	public void setIndexof( String indexof )
	{
		this.indexof = indexof;
	}
	
	/**
	 * 
	 * @param verbose
	 */
	public void setVerbose( String verbose )
	{
		this.verbose = verbose;
	}
	
	/**
	 * 
	 * @param delimeter
	 */
	public void setDelimeter( String delimeter )
	{
		this.delimeter = delimeter;		
	}
	
	/**
	 * 
	 * @param s
	 * @param old
	 * @param replacement
	 * @return
	 */
	public static String replace( String s, String old, String replacement ) 
	{
		if (s==null)
			return "";
		
		int i = s.indexOf(old);
		StringBuffer r = new StringBuffer();
		
		if (i == -1) return s;
			r.append(s.substring(0,i) + replacement);
			
		if (i + old.length() < s.length())
			r.append(replace(s.substring(i + old.length(), s.length()),old, replacement));
			
		return r.toString();
	}	
	
	/**
	 * 
	 */
	public void execute() throws BuildException 
	{
		String prevValue = getProject().getProperty(name);
		String rtn = "";
		
		if (append!=null)
		{
			if ( name == null || name.equals( "" ) ) 
				throw new BuildException( "The 'name' attribute is required with 'stringbuilder'." );
			
//			System.out.println("prevValue:"+prevValue);
//			System.out.println("prevValue:"+prevValue==null);

			if (prevValue==null || prevValue.equals("null"))
				prevValue = "";
			
			if (trim.equals("true"))
			{
				if (append!=null)
					append = append.trim();
			}
			if (prevValue.equals(""))
				rtn = append;
			else
				rtn = prevValue + delimeter + append;
			
			if (verbose.equals("true"))
			{
				System.out.println("'"+ name +"' property value changed, '"+ prevValue +"' to '"+ rtn +"'");
			}
			getProject().setUserProperty(name,rtn);
		}
		else if (replace!=null)
		{
			if ( name == null || name.equals( "" ) ) 
				throw new BuildException( "The 'name' attribute is required with 'stringbuilder'." );
			
			rtn = replace(prevValue,string,replace);
			getProject().setUserProperty(name,rtn);
			if (verbose.equals("true"))
			{
				System.out.println("'"+ name +"' property value changed, '"+ prevValue +"' to '"+ rtn +"'");
			}
		}
		else if (indexof!=null)
		{
			if (prevValue==null || prevValue.equals(""))
				rtn = "-1";
			else
				rtn = Integer.toString(prevValue.indexOf(string));

			getProject().setUserProperty(value,rtn);
		}
		else if (contain!=null)
		{
//			System.out.println("value:"+value);
//			System.out.println("include:"+include);
//			System.out.println("property:"+property);
			
			rtn = (value==null || value.indexOf(contain)==-1) ? "false" : "true";
			getProject().setUserProperty(property,rtn);
		}		
//		if (verbose.equals("true"))
//		{
//			System.out.println(name +" : "+ getProject().getProperty(name));
//			System.out.println("newValue : "+ rtn);
//		}
	}

	public void addTask( Task task ) 
	{
	      tasks.addElement( task );		
	}
}
